const fs = require('fs')
const request = require('request')

function sleep(timeout = 0) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, timeout);
    })
}

// 判断存在
function fileExists(file) {
    return new Promise((resolve,reject)=>{
        fs.access(file, err => {
            if(err) {
                if (err.code === 'ENOENT') {
                    resolve(false)
                } else {
                    reject(err)
                }
            } else {
                resolve(true)
            }
        })
    })
}

function readRemoteFile(url) {
    return new Promise((resolve,reject) => {
        request.get(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                resolve(body)
            } else {
                resolve()
            }
        });
    })
}



module.exports = {
    sleep,
    fileExists,
    readRemoteFile
}